package com.business_clean.app

import android.app.Application
import android.content.res.Configuration
import android.content.res.Resources
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.multidex.MultiDex
import com.blankj.utilcode.util.LogUtils
import com.business_clean.BuildConfig
import com.business_clean.app.config.AppTaskFactory
import com.business_clean.app.config.InitBaidu
import com.business_clean.app.config.InitComm
import com.business_clean.app.config.InitDefault
import com.business_clean.app.config.InitNetWork
import com.business_clean.app.config.InitUtils
import com.business_clean.app.event.AppViewModel
import com.effective.android.anchors.AnchorsManager
import com.effective.android.anchors.Project
import me.hgj.mvvmhelper.base.Ktx.init
import me.jessyan.autosize.AutoSizeConfig

/**
 * 全局配置的全局类型
 */
class App : Application(), ViewModelStoreOwner {


    private var mFactory: ViewModelProvider.Factory? = null
    private lateinit var mAppViewModelStore: ViewModelStore


    companion object {
        lateinit var instance: App
        lateinit var appViewModelInstance: AppViewModel
    }


    override fun onCreate() {
        super.onCreate()
        instance = this
        MultiDex.install(this)
        LogUtils.getConfig().isLogSwitch = true
        init(this, BuildConfig.DEBUG)

        mAppViewModelStore = ViewModelStore()

        //初始化监听器
        appViewModelInstance = getAppViewModelProvider().get(AppViewModel::class.java)

        onMainProcessInit()
    }


    /**
     * @description  代码的初始化请不要放在onCreate直接操作，按照下面新建异步方法
     */
    private fun onMainProcessInit() {
        AnchorsManager.getInstance().debuggable(BuildConfig.DEBUG) //设置锚点
            .addAnchor(
                InitDefault.TASK_ID,
                InitNetWork.TASK_ID,
                InitUtils.TASK_ID,
                InitComm.TASK_ID,
                InitBaidu.TASK_ID,
            ).start(
                Project.Builder("business_clean", AppTaskFactory())
                    .add(InitDefault.TASK_ID)
                    .add(InitNetWork.TASK_ID)
                    .add(InitComm.TASK_ID)
                    .add(InitUtils.TASK_ID)
                    .add(InitBaidu.TASK_ID)
                    .build()
            )
    }




    /**
     * 获取一个全局的ViewModel
     */
    private fun getAppViewModelProvider(): ViewModelProvider {
        return ViewModelProvider(this, this.getAppFactory())
    }

    private fun getAppFactory(): ViewModelProvider.Factory {
        if (mFactory == null) {
            mFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(this)
        }
        return mFactory as ViewModelProvider.Factory
    }

    private fun getCustomViewModelStore(): ViewModelStore {
        return mAppViewModelStore
    }

    override val viewModelStore: ViewModelStore
        get() = getCustomViewModelStore()
}